/* eslint-disable indent */
'use strict'

const Controller = require('egg').Controller
const dayjs = require('dayjs')
const { encrypt, decrypt } = require('../extend/cryptoData')

/**
 * @controller Member 会员模块
 */

class MemberController extends Controller {

    /**
   * @summary 分页查询会员列表
   * @description 分页查询会员列表
   * @router post /v1/member/list
   * @request body MemberList params 分页参数
   * @response 0 MemberInfo
   */
    async list() {
        const { service, helper, request, validate, rule } = this.ctx
        const params = request.body

        // 参数校验
        const passed = await validate.call(this, rule.MemberList, request.body)
        if (!passed) {
            helper.error(null, '参数错误')
            return
        }

        const data = await service.member.getMemberList(params)
        if (data) {
            helper.success(data)
        }
    }

    /**
       * @summary 新建或更新会员信息
       * @description 只有登录后才有权限
       * @router post /v1/member/create_update
       * @request body MemberCreateOrUpdate 参数
       * @response 0 MemberInfo
       */
    async create_update() {
        const { service, helper, request, validate, rule } = this.ctx
        const params = request.body

        // 参数校验
        const passed = await validate.call(this, rule.MemberCreateOrUpdate, request.body)
        if (!passed) {
            helper.error(null, '参数错误')
            return
        }
        const data = await service.member.create_update_Member(params)
        if (data) {
            helper.success(data)
        }
    }

    /**
       * @summary 编辑会员信息
       * @description 只有登录后才有权限
       * @router post /v1/member/edit
       * @request body MemberEdit  参数
       * @response 0 MemberInfo
       */
    async edit() {
        const { service, helper, request, validate, rule } = this.ctx
        const params = request.body

        // 参数校验
        const passed = await validate.call(this, rule.MemberEdit, request.body)
        if (!passed) {
            helper.error(null, '参数错误')
            return
        }
        const data = await service.member.editMember(params)
        if (data) {
            helper.success(data)
        }
    }

    /**
       * @summary 删除会员信息
       * @description 只有登录后才有权限
       * @router post /v1/member/delete
       * @request body MemberDelete 参数
       * @response 0 MemberInfo
       */
    async delete() {
        const { service, helper, request, validate, rule } = this.ctx
        const params = request.body

        // 参数校验
        const passed = await validate.call(this, rule.MemberDelete, request.body)
        if (!passed) {
            helper.error(null, '参数错误')
            return
        }
        const data = await service.member.deleteMember(params)
        if (data) {
            helper.success(data)
        }
    }


    /**
   * @summary 分页查询在线会员列表
   * @description 分页查询在线会员列表
   * @router post /v1/member/online_list
   * @request body OnlineMemberList 分页参数
   * @response 0 OnlineMemberInfo
   */
    async online_list() {
        const { service, helper, request, validate, rule } = this.ctx
        const params = request.body

        // 参数校验
        const passed = await validate.call(this, rule.OnlineMemberList, request.body)
        if (!passed) {
            helper.error(null, '参数错误')
            return
        }

        const data = await service.onlineMember.getOnlineMemberList(params)
        if (data) {
            helper.success(data)
        }
    }

    // ==========================================================================================================

    /**
   * @summary 分页查询充值记录列表
   * @description 分页查询充值记录列表
   * @router post /v1/member/recharge_list
   * @request body RechargeList 分页参数
   * @response 0 RechargeInfo
   */
    async recharge_list() {
        const { service, helper, request, validate, rule } = this.ctx
        const params = request.body

        // 参数校验
        const passed = await validate.call(this, rule.RechargeList, request.body)
        if (!passed) {
            helper.error(null, '参数错误')
            return
        }

        const data = await service.recharge.getRechargeList(params)
        if (data) {
            helper.success(data)
        }
    }


    /**
   * @summary 分页查询上机记录列表
   * @description 分页查询上机记录列表
   * @router post /v1/member/upmachine_list
   * @request body UpmachineRecordList 分页参数
   * @response 0 UpmachineRecordInfo
   */
    async upmachine_list() {
        const { service, helper, request, validate, rule } = this.ctx
        const params = request.body

        // 参数校验
        const passed = await validate.call(this, rule.UpmachineRecordList, request.body)
        if (!passed) {
            helper.error(null, '参数错误')
            return
        }

        const data = await service.upmachineRecord.getUpmachineRecordList(params)
        if (data) {
            helper.success(data)
        }
    }


    /**
   * @summary 分页查询下机记录列表
   * @description 分页查询下机记录列表
   * @router post /v1/member/downmachine_list
   * @request body DownmachineRecordList 分页参数
   * @response 0 DownmachineRecordInfo
   */
    async downmachine_list() {
        const { service, helper, request, validate, rule } = this.ctx
        const params = request.body

        // 参数校验
        const passed = await validate.call(this, rule.DownmachineRecordList, request.body)
        if (!passed) {
            helper.error(null, '参数错误')
            return
        }

        const data = await service.downmachineRecord.getDownmachineRecordList(params)
        if (data) {
            helper.success(data)
        }
    }


    // 上报事件
    async post_event() {
        const { service, helper, request, validate, rule } = this.ctx
        const that = this
        const params = request.body
        const key = this.app.config.member.key
        const iv = this.app.config.member.iv
        const isEncrypt = this.app.config.member.isencrypt

        async function operationData(params) {
            try {
                let ret = params
                if (isEncrypt) {
                    // 参数校验
                    const passed = await validate.call(that, rule.PostEvent, params)
                    if (!passed) {
                        console.log('参数校验失败', params)
                        return null
                    }
                    const decryptData = decrypt(params.data, key, iv)
                    if (!decryptData) {
                        console.log('参数解密失败', params.data)
                        return null
                    }
                    ret = JSON.parse(decryptData)
                }
                return ret
            } catch (error) {
                console.log('解密参数发生错误', error)
                return null
            }
        }

        try {
            switch (params.event) {
                case 'downmachine':
                    {
                        const decryptJson = await operationData(params)
                        if (!decryptJson) {
                            console.log('参数解析错误', params)
                            helper.error(null, '参数解析错误')
                            return
                        }
                        // 参数校验
                        const passed = await validate.call(this, rule.DownmachineRecordCreate, decryptJson)
                        if (!passed) {
                            helper.error(null, '参数错误')
                            return
                        }
                        const data = await service.downmachineRecord.createDownmachineRecord(decryptJson)
                        if (data) {
                            helper.success(data)
                        }
                        break
                    }
                case 'upmachine':
                    {
                        const decryptJson = await operationData(params)
                        if (!decryptJson) {
                            helper.error(null, '参数解析错误')
                            return
                        }
                        // 参数校验
                        const passed = await validate.call(this, rule.UpmachineRecordCreate, decryptJson)
                        if (!passed) {
                            helper.error(decryptJson, '参数错误')
                            return
                        }
                        const data = await service.upmachineRecord.createUpmachineRecord(decryptJson)
                        if (data) {
                            helper.success(data)
                        }
                        break
                    }
                case 'changemachine':
                    {
                        const decryptJson = await operationData(params)
                        if (!decryptJson) {
                            helper.error(null, '参数解析错误')
                            return
                        }

                        // 参数校验
                        const passed = await validate.call(this, rule.ChangeMachine, decryptJson)
                        if (!passed) {
                            helper.error(null, '参数错误')
                            return
                        }

                        const data = await service.onlineMember.createChangeMachine(decryptJson)
                        if (data) {
                            // console.log('换机操作', data)
                            helper.success(data)
                        }
                        break
                    }
                case 'recharge':
                    {
                        const decryptJson = await operationData(params)
                        if (!decryptJson) {
                            helper.error(null, '参数解析错误')
                            return
                        }
                        // 参数校验
                        const passed = await validate.call(this, rule.RechargeRecordCreate, decryptJson)
                        if (!passed) {
                            helper.error(null, '参数错误')
                            return
                        }
                        const data = await service.recharge.createRecharge(decryptJson)
                        if (data) {
                            helper.success(data)
                        }
                        break
                    }
                case 'sync_online_member':
                    {
                        const decryptJson = await operationData(params)
                        if (!decryptJson) {
                            helper.error(null, '参数解析错误')
                            return
                        }
                        // 参数校验
                        if (!decryptJson?.pid || !decryptJson?.members) {
                            helper.error(null, '参数错误')
                            return
                        }
                        const data = await service.onlineMember.syncOnlineMember(decryptJson)
                        if (data) {
                            helper.success(data)
                        }
                        break
                    }
                default:
                    helper.failed(1002, null, '事件类型错误')
                    break
            }
        } catch (error) {
            helper.failed(1002, null, '数据格式错误')
            console.log('错误', error)
        }
    }
}


module.exports = MemberController
